package crmdna.api.endpoint;

import java.util.List;

import javax.annotation.Nullable;
import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.attendance.AttendanceFactory;
import crmdna.attendance.CheckInMemberProp;
import crmdna.attendance.IAttendance;
import crmdna.common.APIException;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Constants;
import crmdna.common.Utils;

@Api(name = "attendance", clientIds = {
		"220866004543.apps.googleusercontent.com",
		com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID })
public class AttendanceApi {
	@ApiMethod(path = "checkin", httpMethod = HttpMethod.POST)
	public APIResponse checkin(@Nullable @Named("client") String client,
			@Named("memberId") long memberId,
			@Named("programId") long programId,
			@Named("sessionDateYYYYMMDD") int sessionDateYYYYMMDD,
			@Named("batchNo") int batchNo, User user) {
		try {
			if (null == client)
				client = Constants.CLIENT_ISHA;

			IAttendance impl = AttendanceFactory.getImpl(client);
			int numCheckins = impl.checkin(memberId, programId,
					sessionDateYYYYMMDD, batchNo, Utils.getLoginEmail(user));

			APIResponse apiResponse = new APIResponse();
			apiResponse.statusCode = Status.SUCCESS;
			apiResponse.object = numCheckins;
			apiResponse.userFriendlyMessage = "Number of checkins: "
					+ numCheckins;

			return apiResponse;
		} catch (APIException ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "checkout", httpMethod = HttpMethod.POST)
	public APIResponse checkout(@Nullable @Named("client") String client,
			@Named("memberId") long memberId,
			@Named("programId") long programId,
			@Named("sessionDateYYYYMMDD") int sessionDateYYYYMMDD, User user) {
		try {
			if (null == client)
				client = Constants.CLIENT_ISHA;

			IAttendance impl = AttendanceFactory.getImpl(client);
			int numCheckins = impl.checkout(memberId, programId,
					sessionDateYYYYMMDD, Utils.getLoginEmail(user));

			APIResponse apiResponse = new APIResponse();
			apiResponse.statusCode = Status.SUCCESS;
			apiResponse.object = numCheckins;
			apiResponse.userFriendlyMessage = "Number of checkins: "
					+ numCheckins;

			return apiResponse;
		} catch (APIException ex) {
			return new APIResponse(ex);
		}

	}

	@ApiMethod(path = "getNumCheckins", httpMethod = HttpMethod.GET)
	public APIResponse getNumCheckins(@Nullable @Named("client") String client,
			@Named("programId") long programId,
			@Named("sessionDateYYYYMMDD") int sessionDateYYYYMMDD,
			@Named("batchNo") int batchNo) {
		try {
			if (null == client)
				client = Constants.CLIENT_ISHA;

			IAttendance impl = AttendanceFactory.getImpl(client);

			int numCheckins = impl.getNumCheckins(programId,
					sessionDateYYYYMMDD, batchNo);

			APIResponse apiResponse = new APIResponse();
			apiResponse.statusCode = Status.SUCCESS;
			apiResponse.object = numCheckins;
			apiResponse.userFriendlyMessage = "Number of checkins: "
					+ numCheckins;
			return apiResponse;

		} catch (APIException ex) {
			return new APIResponse(ex);
		}

	}

	@ApiMethod(path = "getMembersForCheckIn", httpMethod = HttpMethod.GET)
	public APIResponse getMembersForCheckIn(
			@Nullable @Named("client") String client,
			@Named("searchStr") String searchStr,
			@Named("programId") long programId,
			@Named("sessionDateYYYYMMDD") int sessionDateYYYYMMDD,
			@Nullable @Named("maxResultsSize") Integer maxResultSize, User user) {
		try {
			if (null == client)
				client = Constants.CLIENT_ISHA;

			IAttendance impl = AttendanceFactory.getImpl(client);

			List<CheckInMemberProp> checkInMemberProps = impl
					.getMembersForCheckIn(searchStr, programId,
							sessionDateYYYYMMDD, maxResultSize,
							Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, checkInMemberProps);
		} catch (APIException ex) {
			return new APIResponse(ex);
		}
	}
}
